//------------------------------------------------------------------------------
#include <QtGui>
#include <QMessageBox>
#include <QDate>
#include <QSqlRecord>
#include "CFindFrm.h"
#include "CActionFrm.h"
#include "CMySQL.h"
#include "CDAOManager.h"
#include "__CDAOAction.h"
//------------------------------------------------------------------------------
CActionFrm::CActionFrm(QString name, QMap<QString, QVariant> lastKeys, QWidget *parent) : CMyMainWindow(name, parent) {
	setActionCur(CGlobal::acConstruct);
	
	dao=new CDAOAction();
	
	setupUi(this);
	
	setStatusBar(NULL);
	
	loadSchema();
	
	if(CDAOManager::getInstance()->getNbEnregistrement(dao) == 0) {
		setActionCur(CGlobal::acTableVide);
		keyValues["code"]=QVariant();
	}else {
		keyValues["code"]=lastKeys["code"];
		if(!chargeDonnees()) {
			chargeDonnees(CGlobal::sePremier);
		}
	}
}
//------------------------------------------------------------------------------
void CActionFrm::setActionCur(CGlobal::EAction action) {
	actionCur=action;
	
	switch(actionCur) {
		case CGlobal::acNone:
			actPremiereFiche->setEnabled(true);
			actFichePrecedente->setEnabled(true);
			actFicheSuivante->setEnabled(true);
			actDerniereFiche->setEnabled(true);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(true);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(true);
			
			enableChamps(true);
			code->setEnabled(false);
			break;
		case CGlobal::acTableVide:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(false);
			
			clearChamps();
			enableChamps(false);
			creation->setValue("");
			break;
		case CGlobal::acAjout:
			clearChamps();
			initChamps();
			enableChamps(true);
			code->setFocus();
		case CGlobal::acModif:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(true);
			actAnnuler->setEnabled(true);
			
			actRechercheUneFiche->setEnabled(false);
		default:
			break;
	}
}
//------------------------------------------------------------------------------
CGlobal::EAction CActionFrm::getActionCur(void) {
	return actionCur;
}
//------------------------------------------------------------------------------
void CActionFrm::closeEvent(QCloseEvent * event) {
	if(getActionCur() != CGlobal::acNone && getActionCur() != CGlobal::acTableVide) {
		QMessageBox msgBox(	QMessageBox::Warning, tr("Attention"), 
							tr("Veuillez annuler ou valider avant de sortir"), QMessageBox::Ok);
		msgBox.exec();	
		
		event->ignore();
	}else {
		emit quit(objectName(), keyValues);
		event->accept();
	}
}
//------------------------------------------------------------------------------
void CActionFrm::showFormulaire(CGlobal::EAction& action) {
	keyValues["code"]=dao->getFieldCode();
	
	code->setValue(dao->getFieldCode());
	nom->setValue(dao->getFieldNom());
	jourEchPref->setValue(dao->getFieldJourEchPref());
	repete->setValue(dao->getFieldRepete());
	creation->setValue(dao->getFieldCreation());
}
//------------------------------------------------------------------------------
void CActionFrm::on_actAjouter_triggered(void) {
	setActionCur(CGlobal::acAjout);
	
	dao->toNull();
	creation->setValue(CDAOManager::getInstance()->getCurrentDate());
}
//------------------------------------------------------------------------------
void CActionFrm::on_actValider_triggered(void) {
	bool execOk;
	
	code->setText(code->text().toUpper());
	calculChamps(getActionCur());
	
	if(!validChamps()) return;
	if(!validLiaison()) return;
	
	dao->setFieldNom(nom->getValue());
	dao->setFieldJourEchPref(jourEchPref->getValue());
	dao->setFieldRepete(repete->getValue());
	dao->setFieldModification(CDAOManager::getInstance()->getCurrentDate());
	dao->setFieldModificateur(CMySQL::getInstance()->getUser());
	
	switch(getActionCur()) {
		case CGlobal::acAjout:
			dao->setFieldCode(code->getValue());
			dao->setFieldCreation(CDAOManager::getInstance()->getCurrentDate());
			dao->setFieldCreateur(CMySQL::getInstance()->getUser());
			
			execOk=CDAOManager::getInstance()->insert(dao);
			break;
		case CGlobal::acModif:
			execOk=CDAOManager::getInstance()->update(dao);
			break;
		default:
			return;
	}
	
	if(execOk) {
		keyValues["code"]=dao->getFieldCode();
		setActionCur(CGlobal::acNone);
	}
}
//------------------------------------------------------------------------------
void CActionFrm::on_actAnnuler_triggered(void) {
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir annuler ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		if(!chargeDonnees(CGlobal::seNone, false)) {
			if(!chargeDonnees(CGlobal::sePrecedent)) {
				if(!chargeDonnees(CGlobal::seSuivant)) {
					setActionCur(CGlobal::acTableVide);
					return;
				}
			}
		}
	}
}
//------------------------------------------------------------------------------
void CActionFrm::on_actSupprimer_triggered(void) {
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir supprimer la fiche courante ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		if(!CMySQL::getInstance()->canDelete("echeancesClient", "codeAction", keyValues["code"].toString())) {
			return;
		}
		
		CDAOManager::getInstance()->drop(dao, keyValues);
		
		if(!chargeDonnees(CGlobal::sePrecedent)) {
			if(!chargeDonnees(CGlobal::seSuivant)) {
				setActionCur(CGlobal::acTableVide);
			}
		}
	}
}
//------------------------------------------------------------------------------
void CActionFrm::onChange(void) {
	if(getActionCur() == CGlobal::acNone) {
		setActionCur(CGlobal::acModif);
	}
}
//------------------------------------------------------------------------------
void CActionFrm::on_actRechercheUneFiche_triggered(void) {
	CFindFrm *findFrm=new CFindFrm(dao->getTableName(), this);
	
	findFrm->setModal(true);
	findFrm->exec();
	if(findFrm->result() == QDialog::Accepted) {
		keyValues["code"]=QVariant(findFrm->getResult());
		chargeDonnees(CGlobal::seNone);
	}
}
//------------------------------------------------------------------------------
void CActionFrm::onInvokeHelp(CChamp *champ) {
	QMessageBox msgBox(	QMessageBox::Information, champ->getDescription(), 
						QObject::tr("Dans la base: ")+champ->getTable()+"."+champ->getChamp(), QMessageBox::Ok);
	msgBox.exec();	
}
//------------------------------------------------------------------------------